package com.lp.service.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.util.MD5Util;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.cache.api.BaseCacheConstants;
import org.iframework.support.spring.cache.api.BaseCacheManager;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.model.Member;
import com.lp.business.service.MemberService;
import com.lp.service.function.BusinessUtil;

/**
 * 登录注册
 * 
 * @author lipeng
 * @date 2018年08月22日 下午1:15:45
 */
@RestController
@RequestMapping("/api/member")
public class MemberControllerForH5 extends BaseControllerSupport {

	@Autowired
	private MemberService memberService;
	@Autowired
	private BaseCacheManager cacheManager;

	/**
	 * 密码登录 <br>
	 */
	@RequestMapping(value = "loginN", method = { RequestMethod.POST, RequestMethod.GET })
	public Result loginN(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String mobile = params.get("mobile");
			String password = params.get("password");
			if(V.isEmpty(mobile)||V.isEmpty(password)){
				return Result.error("请完整填写用户名和密码");
			}
			Member member = new Member();
			member.setMobile(mobile);
			member.setLoginPwd(MD5Util.MD5Encode(password, 1));
			member.setStatus(RecordStatus.AVAILABLE);
			Member model = this.memberService.findMember(member);
			if (V.isNotEmpty(model)) {
				String token = BusinessUtil.getToken();
				cacheManager.cacheObject("token_"+token, model.getId(), BaseCacheConstants.CACHE_EXP_WEEK);
				Map<String,Object> data = new HashMap<String,Object>();
				data.put("token", token);
				return Result.ok("登录成功").put("data", data);
			} else {
				return Result.error("登录失败");
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}

	/**
	 * 验证码登录 <br>
	 */
	@RequestMapping(value = "loginD", method = { RequestMethod.POST, RequestMethod.GET })
	public Result loginD(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		
		try {
			String mobile = params.get("mobile");
			String code = params.get("code");
			if(V.isEmpty(mobile)||V.isEmpty(code)){
				return Result.error("请完整填写用户名和验证码");
			}
			String codeTemp = (String) this.cacheManager.loadObject("code_"+mobile);
			if (V.isNotEquals(code, codeTemp)) {
				return Result.error("验证码校验失败");
			}
			Member member = new Member();
			member.setMobile(mobile);
			member.setStatus(RecordStatus.AVAILABLE);
			if (V.isNotEmpty(this.memberService.findMember(member))) {
				String token = BusinessUtil.getToken();
				cacheManager.cacheObject("token_"+token, mobile, 30 * 60L);
				Map<String,Object> data = new HashMap<String,Object>();
				data.put("token", token);
				return Result.ok("登录成功").put("data", data);
			} else {
				return Result.error("登录失败，该手机号未注册");
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}

	/**
	 * 注册 <br>
	 */
	@RequestMapping(value = "register", method = { RequestMethod.POST, RequestMethod.GET })
	public Result register(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String mobile = params.get("mobile");
			String password = params.get("password");
			String rePassword = params.get("rePassword");
			String email = params.get("email");
			if(V.isEmpty(mobile)||V.isEmpty(password)||V.isEmpty(rePassword)){
				return Result.error("请完整填写用户名和密码");
			}
			if(V.isNotMobile(mobile)){
				return Result.error("手机号不合法");
			}
			if(V.isNotEquals(password, rePassword)){
				return Result.error("密码和重复密码不一致");
			}
			
			Member member = new Member();
			member.setMobile(mobile);
			member.setStatus(RecordStatus.AVAILABLE);
			if (V.isNotEmpty(this.memberService.findMember(member))) {
				return Result.error("该手机号已经存在");
			}
			member.setMobile(mobile);
			member.setLoginPwd(MD5Util.MD5Encode(password, 1));
			member.setEmail(email);
			if(V.isNotEmpty(memberService.save(member))){
				return Result.ok("注册成功");
			}else{
				return Result.error("注册失败");
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}

	/**
	 * 短信验证码发送 <br>
	 */
	@RequestMapping(value = "sendCode", method = { RequestMethod.POST, RequestMethod.GET })
	public Result sendCode(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String mobile = params.get("mobile");
			if (V.isEmpty(mobile)) {
				return Result.error("手机号码为空");
			}
			if(V.isNotMobile(mobile)){
				return Result.error("手机号不合法");
			}
			String code = BusinessUtil.getRandomString(4);
			this.cacheManager.cacheObject("code_"+mobile, code,5*60L);
			L.i("手机号 "+mobile+" 验证码为："+code);
			//发送验证码，待开发，临时将验证码直接返回给前端
			Map<String,Object> data = new HashMap<String,Object>();
			data.put("code", code);
			return Result.ok("验证码发送成功").put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}

	}

	/**
	 * 重置密码 <br>
	 */
	@RequestMapping(value = "resetPassword", method = { RequestMethod.POST, RequestMethod.GET })
	public Result resetPassword(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String mobile = params.get("mobile");
			String code = params.get("code");
			String password = params.get("password");
			String rePassword = params.get("rePassword");
			if(V.isEmpty(mobile)||V.isEmpty(code)||V.isEmpty(password)||V.isEmpty(rePassword)){
				return Result.error("请完整填写用户名、验证码和密码");
			}
			if(V.isNotMobile(mobile)){
				return Result.error("手机号不合法");
			}
			String codeTemp = (String) this.cacheManager.loadObject("code_"+mobile);
			if (V.isNotEquals(code, codeTemp)) {
				return Result.error("验证码校验失败或已失效");
			}
			if(V.isNotEquals(password, rePassword)){
				return Result.error("密码和重复密码不一致");
			}
			Member member = new Member();
			member.setMobile(mobile);
			member.setStatus(RecordStatus.AVAILABLE);
			member = this.memberService.findMember(member);
			if (V.isNotEmpty(member)) {
				member.setLoginPwd(MD5Util.MD5Encode(password, 1));
				if(memberService.update(member)){
					return Result.ok("重置成功");
				}
			}
			return Result.error("重置失败，没有找到该用户");
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}

	/**
	 * 修改密码 <br>
	 */
	@RequestMapping(value = "updatePassword", method = { RequestMethod.POST, RequestMethod.GET })
	public Result updatePassword(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String mobile = params.get("mobile");
			String password = params.get("password");
			String newPassword = params.get("newPassword");
			String reNewPassword = params.get("reNewPassword");
			if(V.isEmpty(mobile)||V.isEmpty(password)||V.isEmpty(newPassword)||V.isEmpty(reNewPassword)){
				return Result.error("请完整填写用户名和密码");
			}
			if(V.isNotMobile(mobile)){
				return Result.error("手机号不合法");
			}
			if(V.isNotEquals(newPassword, reNewPassword)){
				return Result.error("新密码和重复密码不一致");
			}
			
			Member member = new Member();
			member.setMobile(mobile);
			member.setLoginPwd(MD5Util.MD5Encode(password, 1));
			member.setStatus(RecordStatus.AVAILABLE);
			member = this.memberService.findMember(member);
			if (V.isNotEmpty(member)) {
				member.setLoginPwd(MD5Util.MD5Encode(password, 1));
				if(memberService.update(member)){
					return Result.ok("修改成功");
				}
			}
			return Result.error("修改失败,原密码错误");
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	
}
